function varargout = spyview_cpanel(varargin)
% SPYVIEW_CPANEL MATLAB code for spyview_cpanel.fig
%      SPYVIEW_CPANEL, by itself, creates a new SPYVIEW_CPANEL or raises the existing
%      singleton*.
%
%      H = SPYVIEW_CPANEL returns the handle to a new SPYVIEW_CPANEL or the handle to
%      the existing singleton*.
%
%      SPYVIEW_CPANEL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SPYVIEW_CPANEL.M with the given input arguments.
%
%      SPYVIEW_CPANEL('Property','Value',...) creates a new SPYVIEW_CPANEL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before spyview_cpanel_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to spyview_cpanel_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help spyview_cpanel

% Last Modified by GUIDE v2.5 28-Mar-2012 17:38:18

% Begin initialization code - DO NOT EDIT
gui_Singleton = 0;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @spyview_cpanel_OpeningFcn, ...
                   'gui_OutputFcn',  @spyview_cpanel_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before spyview_cpanel is made visible.
function spyview_cpanel_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to spyview_cpanel (see VARARGIN)

% Choose default command line output for spyview_cpanel
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes spyview_cpanel wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = spyview_cpanel_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function wpcnt_Callback(hObject, eventdata, handles)
% hObject    handle to wpcnt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of wpcnt as text
%        str2double(get(hObject,'String')) returns contents of wpcnt as a double
wpcnt=str2double(get(hObject,'String'));
handles.sv.use_wpcnt=1;
handles.sv.wpcnt=wpcnt;

% --- Executes during object creation, after setting all properties.
function wpcnt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to wpcnt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on slider movement.
function wpcnts_Callback(hObject, eventdata, handles)
% hObject    handle to wpcnts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
wpcnt=get(hObject,'Value');
handles.sv.use_wpcnt=1;
handles.sv.wpcnt=wpcnt;

% --- Executes during object creation, after setting all properties.
function wpcnts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to wpcnts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'ContinuousValueChange',get(hObject,'Callback'));


function bpcnt_Callback(hObject, eventdata, handles)
% hObject    handle to bpcnt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of bpcnt as text
%        str2double(get(hObject,'String')) returns contents of bpcnt as a double
bpcnt=str2double(get(hObject,'String'));
handles.sv.use_bpcnt=1;
handles.sv.bpcnt=bpcnt;


% --- Executes during object creation, after setting all properties.
function bpcnt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bpcnt (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function bpcnts_Callback(hObject, eventdata, handles)
% hObject    handle to bpcnts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
bpcnt=get(hObject,'Value');
handles.sv.use_bpcnt=1;
handles.sv.bpcnt=bpcnt;


% --- Executes during object creation, after setting all properties.
function bpcnts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bpcnts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'ContinuousValueChange',get(hObject,'Callback'));

function update_cpanel(hObject,eventdata,handles)
  handles=guidata(hObject);
  fmt='%4.2g';
  sv=handles.sv;
  set(handles.wpcnt,'String',sprintf(fmt,sv.wpcnt));
  set(handles.wpcnts,'Value',sv.wpcnt);
  set(handles.bpcnt,'String',sprintf(fmt,sv.bpcnt));
  set(handles.bpcnts,'Value',sv.bpcnt);
  
  set(handles.minzs,'Value',sv.minz);
  set(handles.minzs,'Min',min(sv.pms(1),sv.minz));
  set(handles.minzs,'Max',max(sv.pms(end),sv.minz));
  set(handles.minz,'String',sprintf(fmt,sv.minz));
  
  set(handles.maxzs,'Value',sv.maxz);
  set(handles.maxzs,'Min',min(sv.pms(1),sv.maxz));
  set(handles.maxzs,'Max',max(sv.pms(end),sv.maxz));
  set(handles.maxz,'String',sprintf(fmt,sv.maxz));
  
  cen=(sv.minz+sv.maxz)/2;
  wid=(sv.maxz-sv.minz);
  
  set(handles.centerz,'String',sprintf(fmt,cen));
  set(handles.centerzs,'Value',cen);
  set(handles.centerzs,'Min',min(sv.pms(1),cen));
  set(handles.centerzs,'Max',max(sv.pms(end),cen));
  set(handles.widthz,'String',sprintf(fmt,wid));
  set(handles.widthzs,'Value',wid);
  set(handles.widthzs,'Min',min(0,wid));
  set(handles.widthzs,'Max',max(sv.pms(end)-sv.pms(1),wid));
  
  set(handles.gamma,'String',sprintf(fmt,sv.gamma));
  set(handles.gammas,'Value',sv.gamma);
  set(handles.gcenter,'String',sprintf(fmt,sv.gammacenter));
  set(handles.gcenters,'Value',sv.gammacenter);
  if sv.use_bpcnt
     set(handles.bpcntl,'FontWeight','Bold');
     set(handles.minzl,'FontWeight','Normal');
  else
     set(handles.bpcntl,'FontWeight','Normal');
     set(handles.minzl,'FontWeight','Bold');
  end
  if sv.use_wpcnt
     set(handles.wpcntl,'FontWeight','Bold');
     set(handles.maxzl,'FontWeight','Normal');
  else
     set(handles.wpcntl,'FontWeight','Normal');
     set(handles.maxzl,'FontWeight','Bold');
  end
  
  if eventdata  % Update colormaps
      cmapl=handles.sv.listColormaps();
      set(handles.colormap,'String',cmapl);
      if isstr(sv.cmap)
          ind=strmatch(sv.cmap,cmapl,'exact');
          set(handles.colormap,'Value',ind);
      end
  end

% --- Executes on selection change in colormap.
function colormap_Callback(hObject, eventdata, handles)
% hObject    handle to colormap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns colormap contents as cell array
%        contents{get(hObject,'Value')} returns selected item from colormap
cmaps=get(handles.colormap,'String');
cmap=cmaps{get(handles.colormap,'Value')};
handles.sv.cmap = cmap;

% --- Executes during object creation, after setting all properties.
function colormap_CreateFcn(hObject, eventdata, handles)
% hObject    handle to colormap (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function minz_Callback(hObject, eventdata, handles)
% hObject    handle to minz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of minz as text
%        str2double(get(hObject,'String')) returns contents of minz as a double
val=str2double(get(hObject,'String'));
handles.sv.use_bpcnt=0;
handles.sv.minz=val;


% --- Executes during object creation, after setting all properties.
function minz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function minzs_Callback(hObject, eventdata, handles)
% hObject    handle to minzs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val=get(hObject,'Value');
handles.sv.use_bpcnt=0;
handles.sv.minz=val;

% --- Executes during object creation, after setting all properties.
function minzs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minzs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'ContinuousValueChange',get(hObject,'Callback'));



function maxz_Callback(hObject, eventdata, handles)
% hObject    handle to maxz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxz as text
%        str2double(get(hObject,'String')) returns contents of maxz as a double
val=str2double(get(hObject,'String'));
handles.sv.use_wpcnt=0;
handles.sv.maxz=val;

% --- Executes during object creation, after setting all properties.
function maxz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function maxzs_Callback(hObject, eventdata, handles)
% hObject    handle to maxzs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val=get(hObject,'Value');
handles.sv.use_wpcnt=0;
handles.sv.maxz=val;

% --- Executes during object creation, after setting all properties.
function maxzs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxzs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'ContinuousValueChange',get(hObject,'Callback'));



function centerz_Callback(hObject, eventdata, handles)
% hObject    handle to centerz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of centerz as text
%        str2double(get(hObject,'String')) returns contents of centerz as a double
val=str2double(get(hObject,'String'));
handles.sv.use_wpcnt=0;
handles.sv.use_bpcnt=0;
wid=(handles.sv.maxz - handles.sv.minz)/2;
handles.sv.maxz=val+wid/2;
handles.sv.minz=val-wid/2;

% --- Executes during object creation, after setting all properties.
function centerz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to centerz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function centerzs_Callback(hObject, eventdata, handles)
% hObject    handle to centerzs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val=get(hObject,'Value');
handles.sv.use_wpcnt=0;
handles.sv.use_bpcnt=0;
wid=(handles.sv.maxz - handles.sv.minz);
handles.sv.maxz=val+wid/2;
handles.sv.minz=val-wid/2;

% --- Executes during object creation, after setting all properties.
function centerzs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to centerzs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'ContinuousValueChange',get(hObject,'Callback'));



function widthz_Callback(hObject, eventdata, handles)
% hObject    handle to widthz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of widthz as text
%        str2double(get(hObject,'String')) returns contents of widthz as a double
val=str2double(get(hObject,'String'));
handles.sv.use_wpcnt=0;
handles.sv.use_bpcnt=0;
cen=(handles.sv.maxz + handles.sv.minz)/2;
handles.sv.maxz=cen+val/2;
handles.sv.minz=cen-val/2;

% --- Executes during object creation, after setting all properties.
function widthz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to widthz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function widthzs_Callback(hObject, eventdata, handles)
% hObject    handle to widthzs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
val=get(hObject,'Value');
handles.sv.use_wpcnt=0;
handles.sv.use_bpcnt=0;
cen=(handles.sv.maxz + handles.sv.minz)/2;
handles.sv.maxz=cen+val/2;
handles.sv.minz=cen-val/2;


% --- Executes during object creation, after setting all properties.
function widthzs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to widthzs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'ContinuousValueChange',get(hObject,'Callback'));



function gamma_Callback(hObject, eventdata, handles)
% hObject    handle to gamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of gamma as text
%        str2double(get(hObject,'String')) returns contents of gamma as a double


% --- Executes during object creation, after setting all properties.
function gamma_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
handles.sv.gamma = str2double(get(hObject,'String'));

% --- Executes on slider movement.
function gammas_Callback(hObject, eventdata, handles)
% hObject    handle to gammas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
handles.sv.gamma = get(hObject,'Value');

% --- Executes during object creation, after setting all properties.
function gammas_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gammas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'ContinuousValueChange',get(hObject,'Callback'));



function gcenter_Callback(hObject, eventdata, handles)
% hObject    handle to gcenter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of gcenter as text
%        str2double(get(hObject,'String')) returns contents of gcenter as a double
handles.sv.gammacenter = str2double(get(hObject,'String'));

% --- Executes during object creation, after setting all properties.
function gcenter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gcenter (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function gcenters_Callback(hObject, eventdata, handles)
% hObject    handle to gcenters (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
handles.sv.gammacenter = get(hObject,'Value');


% --- Executes during object creation, after setting all properties.
function gcenters_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gcenters (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
addlistener(hObject,'ContinuousValueChange',get(hObject,'Callback'));
